/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package spartpartsmanager;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
import java.util.StringTokenizer;

/**
 *
 * @author Ngocvan
 */
public class SparePartManager {

    private static final int MIN_BOUND = 1000;
    private static final int MAX_BOUND = 100000;
    BufferedReader bfRr = new BufferedReader(new InputStreamReader(System.in));
    
    private static void searchOption() {
        
    }

    private static void getPriceOption() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    private static void printOption() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    private static void getQuantityOption() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    private Hashtable dicco;
    private ArrayList<String> machineList;

    private static void printMenu() {
        System.out.println("SPart Manager Menu");
        System.out.print("1.Search \t 2.Get Price \t 3.Print \t 4.Get Quantity \t 5.Exit");
        System.out.print("Please choose: ");
    }
//    public ArrayList<SPart> listParts;
//    public String fileName;

    public SparePartManager() {
//        this.fileName = fileName;
        this.dicco = new Hashtable();
        this.machineList = new ArrayList<>();
    }

    //read file -> hash map [key] -> [price]/ [key] -> [arraylist<String>]
    public void readFile(String fileName) throws FileNotFoundException,
            IOException,
            WrongFormatFileException,
            NumberFormatException,
            MissingWordException,
            WrongPriceException {
        ArrayList<String> words = new ArrayList<>();
        String machine;
        this.dicco.clear();
//        try {
        BufferedReader br = new BufferedReader(new FileReader(fileName));
//        StringTokenizer sTkr = new StringTokenizer();
        String line;
//        them cac bo phan cua may vao tu dien
        machine = br.readLine();
        StringTokenizer mToken = new StringTokenizer(machine);
        while (mToken.hasMoreElements()){
            words.add(mToken.nextElement().toString());
            this.machineList.add(mToken.nextElement().toString());
        }
        System.out.println(machine);
        
        //doc cac dong lay dinh nghia cac tu
        while ((line = br.readLine()) != null) {
//            System.out.println(line);
            StringTokenizer sTkr = new StringTokenizer(line);
            if (line.startsWith("D")) {
                String key = sTkr.nextElement().toString();
                //loai bo tu tu words:danh sach cac tu chua co dinh nghia
                while (words.contains(key)) words.remove(key);
                
                int value = Integer.parseInt(sTkr.nextElement().toString());
                if (value < 0) {
                    throw new WrongPriceException("The price is negative, check again");
                } else if ((value < MIN_BOUND) || (value > MAX_BOUND)) {
                    throw new WrongPriceException("The price is out of bound");
                }
//                System.out.println(key + "...." + value);
                dicco.put(key, value);
//               
            } else if (line.startsWith("F")) {
                String key = sTkr.nextElement().toString();
                while (words.contains(key)) words.remove(key);
                
                ArrayList<String> values = new ArrayList<String>();
                while (sTkr.hasMoreElements()) {
                    String word = sTkr.nextElement().toString();
//                    values.add(sTkr.nextElement().toString());
                    if (!dicco.containsKey(word)) words.add(word);
                    values.add(word);
                }
//                System.out.println(key + "..." + values.size() + values.get(0));
                dicco.put(key, values);
            } else {
                throw new WrongFormatFileException("Wrong format file exception");
            }
        }

        //check dictionary
        if (!words.isEmpty()) throw new MissingWordException();
        //print out dicco
        Map<String, Object> map = dicco;
        Iterator<Map.Entry<String, Object>> it = map.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<String, Object> entry = it.next();
            System.out.println(entry.getKey() + "->>" + entry.getValue());
            
        }
        System.out.println("Tu dien bi thieu? " + !words.isEmpty());
        
//        } catch (FileNotFoundException fnfe) {
//            System.err.println("FileNotFound Exception");
//        } catch (IOException ioe) {
//            System.err.println("IO Exception");
//        } catch (WrongFormatFileException wffe){
//            System.err.println(wffe.toString());
//        } catch (NumberFormatException cce){
//            System.err.println("May be the price is not a number");
//        } catch (MissingWordException mwe) {
//            System.err.println(mwe.toString());
//        } catch (WrongPriceException wpe) {
//            System.err.println(wpe.toString());
//        }

    }

    
//    public int getPrice(SPart)
    
        /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws FileNotFoundException, 
            IOException,
            WrongFormatFileException,
            NumberFormatException,
            MissingWordException,
            WrongPriceException,
            InterruptedException {
        // TODO code application logic here
        SparePartManager man = new SparePartManager();
        man.readFile("data.txt");
        
        
        int option = 1;
        while (option != 5) {
            try {
                printMenu();
                option = Integer.parseInt(bfRr.readLine());
            } catch (NumberFormatException e) {
                System.err.println("Please input number, not a letter");
            }
            switch (option) {
                case 1:
                    searchOption();
                    break;
                case 2:
                    getPriceOption();
                    break;
                case 3:
                    printOption();
                    break;
                case 4:
                    getQuantityOption();
                default:
                    System.out.println("Please enter your choice");
            }
        }
        System.out.println("Good bye !!!...");
        Thread.sleep(500);
    }
}
